export default class Background{
    constructor(options){
        this.ctx = null;
        this.width = options.width;
        this.height = options.height;
        this.image = options.image;
        this.speed = options.speed;
        this.y = options.y;
        this.x = 0;
    }
    render(){
        // 1.获取图片数量
        let imgNum = Math.ceil(this.ctxW/this.width) * 2;
        // 2.循环绘制图片
        for (let i = 0; i < imgNum; i++) {
            this.ctx.drawImage(this.image,this.x + i*this.width,this.y,this.width,this.height)
        }
    }
    update(){
        this.x -= this.speed;
        let midX = -1 * Math.ceil(this.ctxW/this.width) * this.width;
        if(this.x <= midX){
            this.x = 0;
        }
    }
    bindCtx(ctx){
        
        this.ctx = ctx;
        this.ctxW = this.ctx.canvas.width
        this.ctxH = this.ctx.canvas.height
        return this;
    }
    pause(){
        this.speed = 0;
    }
}